package com.yun.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.reggie.common.CustomException;
import com.yun.reggie.common.R;
import com.yun.reggie.dto.DishDto;
import com.yun.reggie.entity.Dish;
import com.yun.reggie.entity.DishFlavor;
import com.yun.reggie.entity.SetmealDish;
import com.yun.reggie.mapper.DishMapper;
import com.yun.reggie.service.DishFlavorService;
import com.yun.reggie.service.DishService;
import com.yun.reggie.service.SetmealDishService;
import com.yun.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @author xiao
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;
    /**
     * 添加菜品，同时添加口味。需要操作两张表
     * @param dishDto
     */
    @Override
    public void addWithFlavors(DishDto dishDto) {
        //添加菜品，因为DishDto继承了Dish
        this.save(dishDto);
        //添加完菜品之后，mybatis-plus会自动给添加的类，赋上id值
        Long dishId = dishDto.getId();

        //添加口味,需要注意DishFlavor中dishId属性没有自动赋值
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor:flavors) {
            dishFlavor.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 逻辑和上面的添加是一样的
     * @param dishDto
     */
    @Override
    public void updateWithFlavors(DishDto dishDto) {
        //修改菜品表
        this.updateById(dishDto);
        //修改口味表,可以先把原来的口味数据删除，再添加新的口味
        //根据菜品id删除口味，而不是根据口味id删除
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(wrapper);

        //添加新的口味,注意DishFlavor中dishId没有设置
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor:flavors) {
            flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据dishId删除菜品，根据dishId删除口味
     * 这里就不使用逻辑删除
     * @param ids
     */
    @Override
    public void deleteById(Long ids) {
        //删除菜品之前，需要判断是否有套餐关联，否则抛出业务异常
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(SetmealDish::getDishId,ids);
        int count = setmealDishService.count(wrapper1);
        if (count > 0 ){
            throw new CustomException("当前菜品关联了套餐，不能删除！");
        }
        //删除菜品
        super.removeById(ids);

        //根据菜品id删除口味，而不是根据口味id删除
        LambdaQueryWrapper<DishFlavor> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(DishFlavor::getDishId,ids);
        dishFlavorService.remove(wrapper2);
    }

    /**
     * 批量修改菜品状态
     * @param status
     * @param ids
     */
    @Override
    public void updateSalesStatus(int status, List<Long> ids) {
        LambdaUpdateWrapper<Dish> wrapper = new LambdaUpdateWrapper<>();
        //设置修改条件
        wrapper.in(Dish::getId,ids);
        //设置修改的字段
        wrapper.set(Dish::getStatus,status);
        this.update(wrapper);
    }

}
